﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 1. Write functions for working with shapes in standard Planar coordinate system
		* Points are represented by coordinates P(X, Y)
		* Lines are represented by two points, marking their beginning and ending
			** L(P1(X1,Y1);P2(X2,Y2))
		* Calculate the distance between two points
		* Check if three segment lines can form a triangle -->
	<style>
		button {
			width: 800px;
		}

		fieldset {
			width: 220px;
			display: inline-block;
			margin-right: 25px;
		}

		input {
			width: 40px;
			margin-left: 5px;
		}
	</style>
</head>
<body>
	<fieldset>
		<legend>Line 1</legend>
		<input id="x01" type="text" placeholder="x1" />
		<input id="y01" type="text" placeholder="y1" />
		<input id="x02" type="text" placeholder="x2" />
		<input id="y02" type="text" placeholder="y2" />
	</fieldset>
	<fieldset>
		<legend>Line 2</legend>
		<input id="x11" type="text" placeholder="x1" />
		<input id="y11" type="text" placeholder="y1" />
		<input id="x12" type="text" placeholder="x2" />
		<input id="y12" type="text" placeholder="y2" />
	</fieldset>
	<fieldset>
		<legend>Line 3</legend>
		<input id="x21" type="text" placeholder="x1" />
		<input id="y21" type="text" placeholder="y1" />
		<input id="x22" type="text" placeholder="x2" />
		<input id="y22" type="text" placeholder="y2" />
	</fieldset>
	<button onclick="calculateAll()">Calculate Distance & Possible Triangle</button>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		// Class Point
		function Point(x, y) {
			this.x = x;
			this.y = y;
			this.toString = function () { return "Point(" + this.x + "," + this.y + ")"; }
		}

		// Class Line
		function Line(p1, p2) {
			this.p1 = p1;
			this.p2 = p2;
			this.toString = function () { return "Line[" + this.p1 + ";" + this.p2 + "]"; }
		}

		function calculateDistance(p1, p2) {
			var result = 0.0;

			result = Math.pow(p1.x - p2.x, 2) +
						Math.pow(p1.y - p2.y, 2);

			return Math.sqrt(result);
		}

		function canFormTriangle(l1, l2, l3) {
			var result = true;

			var a = calculateDistance(l1.p1, l1.p2);
			var b = calculateDistance(l2.p1, l2.p2);
			var c = calculateDistance(l3.p1, l3.p2);

			if ((a + b) <= c) {
				result = false;
			}

			if ((a + c) <= b) {
				result = false;
			}

			if ((c + b) <= a) {
				result = false;
			}

			return result;
		}

		function calculateAll() {
			var lines = new Array(3);
			for (var i = 0; i < 3; i++) {
				var x = jsConsole.readInteger("#x" + i + "1");
				var y = jsConsole.readInteger("#y" + i + "1");
				var pt1 = new Point(x, y);

				x = jsConsole.readInteger("#x" + i + "2");
				y = jsConsole.readInteger("#y" + i + "2");
				var pt2 = new Point(x, y);
				
				lines[i] = new Line(pt1, pt2);
				jsConsole.writeLine("For " + lines[i]);
				jsConsole.writeLine("Distance = " + calculateDistance(pt1, pt2));
			}

			jsConsole.writeLine("Is triangle with these 3 lines possible = " +
								canFormTriangle(lines[0], lines[1], lines[2]));
		}
	</script>
</body>
</html>
